﻿#include "readfile.h"

ReadFile::ReadFile(QObject *parent)
    : QObject{parent}
{}

void ReadFile::readFile(QXlsx::Document* outFile, QString iFilePath, QString resFilePath, int start, int end, QVector<COLUMN_MAP> dataVector)
{
    QXlsx::Document iFile(iFilePath);
    QXlsx::Document resFile(resFilePath);

    int rowCount = iFile.dimension().rowCount();

    // 遍历查找文件每一行
    int outRow = 1;
    for(int row = start; row <= end; ++row){
        m_mutex.lock();
        int count = 0;
        for(int inputRow = 1; inputRow <= rowCount; ++inputRow){
            int flag = true;
            for(auto const & data : std::as_const(dataVector)){
                if(data.core_flag && resFile.read(row, data.res_col) != iFile.read(inputRow, data.src_col)){
                    flag = false;
                    break;
                }
            }

            // 找到匹配行
            if(flag){
                for(auto const & data : std::as_const(dataVector)){
                    outFile->write(outRow, data.res_col, resFile.read(row, data.res_col));
                }
                ++count;
                ++outRow;
            }
        }
        // 该数据一行都未找到，仅写入查找核心列内容
        if(count == 0) {
            for(auto const & data : std::as_const(dataVector)){
                if(data.core_flag)
                outFile->write(outRow, data.res_col, resFile.read(row, data.res_col));
            }
            ++outRow;
        }
    }
    qDebug()<<QThread::currentThread()<<u8"完成";

    emit finishSignal();

}
